//
//  DrawVC.m
//  Cat
//
//  Created by dllo on 15/11/26.
//  Copyright (c) 2015年 dllo. All rights reserved.
//
// http://api.huapipi.com/home/painting/navi?deviceId=1826162&uid=1033268

#import "DrawVC.h"
#import "CYHDrawCell.h"
#import "DrawModel.h"
#import "SXCircleLayout.h"
#import "CYHDrawNewVC.h"
#import "DrawWinVC.h"
#import "DrawAllVC.h"
#import "DrawTeachVC.h"
#import "DrawSourceVC.h"
#import "DrawDraw.h"

@interface DrawVC ()<UICollectionViewDataSource, UICollectionViewDelegate, RollViewDelegate>

@property (nonatomic, strong) UICollectionView *DrawCollectionView;
@property (nonatomic, strong) NSMutableArray *rollArr;
@property (nonatomic, strong) NSMutableArray *collectionArr;
@property (nonatomic, strong) RollView *rollView;

@end

@implementation DrawVC

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.rollArr = [NSMutableArray array];
        self.collectionArr = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createView];
    
    [self createData];
}



#pragma mark - createData
- (void)createData {
    [ToolNetWorking ToolNetWorking:@"http://api.huapipi.com/home/painting/navi?deviceId=1826162&uid=1033268" block:^(id result) {
        
        // 轮播图
        NSMutableArray *arr = [NSMutableArray array];
        arr = [DrawModel BaseModel:result[@"bannerList"]];
        for (DrawModel *model in arr) {
            [self.rollArr addObject:model.picUrl];
        }
        
        // collectionView
        self.collectionArr = [DrawModel BaseModel:result[@"rows"]];
        [self.rollView.RollCollcetionView reloadData];
        [self.DrawCollectionView reloadData];
    }];
}



#pragma mark - createView
- (void)createView {
    
    // 轮播图
    self.rollView = [[RollView alloc] initWithFrame:CGRectMake(0, 64, VIEW_WIDTH, VIEW_HEIGHT / 3) imageArr:self.rollArr];
    [self.view addSubview:self.rollView];
    self.rollView.RollDelegate = self;
    self.rollView.RollCollcetionView.scrollEnabled = NO;
    
    
    // collectionView
    SXCircleLayout *flowLayout = [[SXCircleLayout alloc] init];
    
    self.DrawCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, VIEW_HEIGHT / 2.2, VIEW_WIDTH, VIEW_HEIGHT / 2.5) collectionViewLayout:flowLayout];
    self.DrawCollectionView.backgroundColor = [UIColor whiteColor];
    self.DrawCollectionView.delegate = self;
    self.DrawCollectionView.dataSource = self;
    [self.view addSubview:self.DrawCollectionView];
    
    [self.DrawCollectionView registerClass:[CYHDrawCell class] forCellWithReuseIdentifier:@"Draw"];
}



#pragma mark - collectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.collectionArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CYHDrawCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Draw" forIndexPath:indexPath];
    
    DrawModel *model = self.collectionArr[indexPath.row];
    [cell.brandImageView sd_setImageWithURL:[NSURL URLWithString:model.url] placeholderImage:[UIImage imageNamed:@"ooopic_1445565008@2x.png"]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.item == 0) {
        CYHDrawNewVC *NewVC = [[CYHDrawNewVC alloc] init];
        [self.navigationController pushViewController:NewVC animated:YES];
    } else if (indexPath.item == 1) {
        DrawWinVC *winVC = [[DrawWinVC alloc] init];
        [self.navigationController pushViewController:winVC animated:YES];
    } else if (indexPath.item == 2) {
        DrawAllVC *AllVC = [[DrawAllVC alloc] init];
        [self.navigationController pushViewController:AllVC animated:YES];
    } else if (indexPath.item == 5) {
        DrawTeachVC *teachVC = [[DrawTeachVC alloc] init];
        [self.navigationController pushViewController:teachVC animated:YES
         ];
    } else if (indexPath.item == 3) {
        DrawSourceVC *sourceVC = [[DrawSourceVC alloc] init];
        [self.navigationController pushViewController:sourceVC animated:YES];
    } else if (indexPath.item == 4) {
        DrawDraw *drawVC = [[DrawDraw alloc] init];
        [self.navigationController pushViewController:drawVC animated:YES];
    }
}

- (void)pushViewController:(NSIndexPath *)index {
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self network];
}  

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
